﻿//==================================================================
// Copyright (C) 2011 深圳市中旭企业管理股份有限公司
// 文件名: DesEncrypt.cs
// 作 者：HeXingXiang
// 日 期：2011/03/06
// 描 述：DES加密/解密类
// 版 本：2.00
// 修改历史纪录
// 版 本  修改时间      修改人            修改内容
// 2.00   2011/03/06   HeXingXiang       无
//==================================================================

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Security.Cryptography;

namespace ZX.Common.File
{
    /// <summary>
    /// DES加密/解密操作类
    /// </summary>
    public class DesEncrypt
    {
        #region ======== 加密操作 ========
        /// <summary>
        /// DES加密
        /// </summary>
        /// <param name="strTxt">需要加密字符串数据</param>
        /// <returns></returns>
		public static string Encrypt(string strTxt) 
		{
            return Encrypt(strTxt, "hexingxiang");
		}
		/// <summary> 
		/// 加密数据 
		/// </summary> 
        /// <param name="strTxt">需要加密字符串数据</param> 
		/// <param name="sKey">加密钥匙</param> 
		/// <returns>返回加密后字符串</returns> 
        private static string Encrypt(string strTxt, string sKey) 
		{ 
			DESCryptoServiceProvider des = new DESCryptoServiceProvider(); 
			byte[] inputByteArray;
            inputByteArray = Encoding.Default.GetBytes(strTxt); 
			des.Key = ASCIIEncoding.ASCII.GetBytes(System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(sKey, "md5").Substring(0, 8)); 
			des.IV = ASCIIEncoding.ASCII.GetBytes(System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(sKey, "md5").Substring(0, 8)); 
			System.IO.MemoryStream ms=new System.IO.MemoryStream(); 
			CryptoStream cs=new CryptoStream(ms,des.CreateEncryptor(),CryptoStreamMode.Write); 
			cs.Write(inputByteArray,0,inputByteArray.Length); 
			cs.FlushFinalBlock(); 
			StringBuilder ret=new StringBuilder(); 
			foreach( byte b in ms.ToArray()) 
			{ 
				ret.AppendFormat("{0:X2}",b); 
			} 
			return ret.ToString(); 
		} 
		#endregion
		
		#region ======== 解密操作 ========
        /// <summary>
        /// DES解密
        /// </summary>
        /// <param name="strTxt">需要解密字符串数据</param>
        /// <returns>返回解密后字符串</returns>
        public static string Decrypt(string strTxt) 
		{
            return Decrypt(strTxt, "hexingxiang");
		}
		/// <summary> 
		/// 解密数据 
		/// </summary> 
        /// <param name="strTxt">需要解密字符串数据</param> 
        /// <param name="sKey">加密钥匙</param> 
        /// <returns>返回解密后字符串</returns> 
        private static string Decrypt(string strTxt, string sKey) 
		{
			DESCryptoServiceProvider des = new DESCryptoServiceProvider(); 
			int len;
            len = strTxt.Length / 2; 
			byte[] inputByteArray = new byte[len]; 
			int x,i; 
			for(x=0;x<len;x++) 
			{
                i = Convert.ToInt32(strTxt.Substring(x * 2, 2), 16); 
				inputByteArray[x]=(byte)i; 
			} 
			des.Key = ASCIIEncoding.ASCII.GetBytes(System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(sKey, "md5").Substring(0, 8)); 
			des.IV = ASCIIEncoding.ASCII.GetBytes(System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(sKey, "md5").Substring(0, 8)); 
			System.IO.MemoryStream ms=new System.IO.MemoryStream(); 
			CryptoStream cs=new CryptoStream(ms,des.CreateDecryptor(),CryptoStreamMode.Write); 
			cs.Write(inputByteArray,0,inputByteArray.Length); 
			cs.FlushFinalBlock(); 
			return Encoding.Default.GetString(ms.ToArray()); 
		}
		#endregion 
    }
}
